package cn.hdu.liang.ch8;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PC {
    public static void main(String[] args) {
        final BlockingQueue<Character> bq = new ArrayBlockingQueue<>(10);
        final ExecutorService executor = Executors.newFixedThreadPool(2);

        Runnable producer = () -> {
            for(char ch = 'A'; ch <= 'G'; ch++) {
                try {
                    bq.put(ch);
                    System.out.printf("生产了%c%n", ch);
                } catch(InterruptedException ie) {

                }
            }
        };
        executor.execute(producer);

        Runnable consumer = () -> {
            char ch = '\0';
            do {
                try {
                    ch = bq.take();
                    System.out.printf("消费了%c%n", ch);
                } catch(InterruptedException ie) {

                }
            } while(ch != 'G');
            executor.shutdownNow();
        };
        executor.execute(consumer);
    }
}
